package com.example.common.utils;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.common.interfaces.ActivityResultCallback;

public class ProcessFragment extends Fragment {

    private Context mContext;
    private Runnable mPermissionCallback;
    private ActivityResultCallback mActivityResultCallback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    /**
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (isAllGranted(permissions, grantResults)) {
            if (mPermissionCallback != null) {
                mPermissionCallback.run();
            }
        }
        mPermissionCallback = null;
    }

    /**
     * 检查是否具有权限
     * @param permissions
     * @return true：是
     */
    private boolean checkPermissions(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 申请权限是否被允许
     * @return true：是
     */
    private boolean isAllGranted(String[] permissions,int[] grantResults) {
        for(int i=0;i<grantResults.length;i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                //权限被拒绝
                showTip(permissions[i]);
                return false;
            }
        }
        return true;
    }

    /**
     * 权限被拒绝提示
     * @param permission
     */
    private void showTip(String permission) {
        switch (permission) {
            case Manifest.permission.READ_EXTERNAL_STORAGE:
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                //文件读写
                break;
            case Manifest.permission.CAMERA:
                //使用摄像头
                break;
            case Manifest.permission.RECORD_AUDIO:
                //使用麦克风
                break;
            case Manifest.permission.ACCESS_COARSE_LOCATION:
                //定位
                //清除手机位置信息
                break;
            case Manifest.permission.READ_PHONE_STATE:
                //读取手机信息
                break;
        }
    }

    /**
     * 请求权限
     * @param permissions 权限数组
     * @param runnable
     */
    public void requestPermissions(String[] permissions, Runnable runnable) {
        if (runnable != null) {
            if (checkPermissions(permissions)) {
                runnable.run();
            }else {
                mPermissionCallback = runnable;
                requestPermissions(permissions, 0);
            }
        }
    }

    public void startActivityForResult(Intent intent,ActivityResultCallback callback) {
        mActivityResultCallback = callback;
        super.startActivityForResult(intent, 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (mActivityResultCallback != null) {
            if (resultCode != -1) { //RESULT_OK
                mActivityResultCallback.onSuccess(data);
            } else {
                mActivityResultCallback.onFailure();
            }
        }
    }

    /**
     * 释放
     */
    public void release() {
        mPermissionCallback = null;
        mActivityResultCallback = null;
    }

}
